/*
 *    This file is part of SSHTunneler.
 *
 *    Copyright 2009 Jean-Sebastien Gelinas <calestar@gmail.com>
 *
 *    SSHTunneler is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    SSHTunneler is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with SSHTunneler.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package sshtunneler.packager.gui;

import java.awt.event.ActionEvent;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import sshtunneler.library.ExtensionFilter;
import sshtunneler.packager.data.Config;
import sshtunneler.packager.data.TunnelerConfig;
import sshtunneler.packager.gui.frames.ManageServersTunnels;
import sshtunneler.packager.gui.frames.MyActionListener;
import sshtunneler.packager.gui.frames.MyInternalFrameActionDispatcherFrame;
import sshtunneler.packager.gui.frames.ShowFiles;
import sshtunneler.packager.gui.frames.ShowPaths;
import sshtunneler.packager.gui.frames.ShowScript;
import sshtunneler.packager.gui.frames.ShowScriptVariables;
import sshtunneler.packager.installergenerator.InstallerGenerator;

public class CreateTunnelerConfig extends MyInternalFrameActionDispatcherFrame
		implements MyActionListener {
	private static final long serialVersionUID = 7844983172788280787L;
	private ShowPaths showPaths_frame = null;
	private ShowScript showScript_frame = null;
	private ShowScriptVariables showScriptVariables_frame = null;
	private ShowFiles showFiles_frame = null;
	private ManageServersTunnels serverTunnel_frame;

	public CreateTunnelerConfig() {
		this.initComponents();
		this.setTitle("SSHTunneler Packager");
		this.add_config_listener(this);
		this.refresh();
	}

	private void initComponents() {

		this.jScrollPane1 = new javax.swing.JScrollPane();
		this.desktop = new javax.swing.JDesktopPane();
		this.jMenuBar1 = new javax.swing.JMenuBar();
		this.jMenu1 = new javax.swing.JMenu();
		this.mnu_generate = new javax.swing.JMenuItem();
		this.jSeparator1 = new javax.swing.JSeparator();
		this.mnu_exit = new javax.swing.JMenuItem();
		this.jMenu3 = new javax.swing.JMenu();
		this.mnu_program_update = new javax.swing.JCheckBoxMenuItem();
		this.jSeparator4 = new javax.swing.JSeparator();
		this.mnu_ask_username = new javax.swing.JCheckBoxMenuItem();
		this.mnu_overwrite_all = new javax.swing.JCheckBoxMenuItem();
		this.mnu_start_minimized = new javax.swing.JCheckBoxMenuItem();
		this.mnu_connect_to_all = new javax.swing.JCheckBoxMenuItem();
		this.mnu_hide_tool_config = new javax.swing.JCheckBoxMenuItem();
		this.jSeparator5 = new javax.swing.JSeparator();
		this.mnu_debug_mode = new javax.swing.JCheckBoxMenuItem();
		this.jMenu4 = new javax.swing.JMenu();
		this.mnu_show_files = new javax.swing.JMenuItem();
		this.jMenu2 = new javax.swing.JMenu();
		this.mnu_show_variables = new javax.swing.JMenuItem();
		this.jSeparator3 = new javax.swing.JSeparator();
		this.mnu_show_script = new javax.swing.JMenuItem();
		this.jMenu5 = new javax.swing.JMenu();
		this.mnu_show_paths = new javax.swing.JMenuItem();
		this.jSeparator2 = new javax.swing.JSeparator();
		this.jMenuItem3 = new javax.swing.JMenuItem();
		this.jMenuItem4 = new javax.swing.JMenuItem();

		this
				.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		this.setName("Form");

		this.jScrollPane1.setName("jScrollPane1");

		this.desktop.setName("desktop");
		this.jScrollPane1.setViewportView(this.desktop);

		this.getContentPane().add(this.jScrollPane1,
				java.awt.BorderLayout.CENTER);

		this.setPreferredSize(new java.awt.Dimension(600, 600));

		this.jMenuBar1.setName("jMenuBar1");

		this.jMenu1.setText("File");
		this.jMenu1.setName("jMenu1");

		this.mnu_generate.setText("Generate");
		this.mnu_generate.setName("mnu_generate");
		this.mnu_generate
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						CreateTunnelerConfig.this
								.mnu_generateActionPerformed(evt);
					}
				});
		this.jMenu1.add(this.mnu_generate);

		this.jSeparator1.setName("jSeparator1");
		this.jMenu1.add(this.jSeparator1);

		this.mnu_exit.setText("Exit");
		this.mnu_exit.setName("mnu_exit");
		this.mnu_exit.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				CreateTunnelerConfig.this.mnu_exitActionPerformed(evt);
			}
		});
		this.jMenu1.add(this.mnu_exit);

		this.jMenuBar1.add(this.jMenu1);

		this.jMenu3.setText("Config");
		this.jMenu3.setName("jMenu3");

		this.mnu_program_update.setText("Program update");
		this.mnu_program_update.setName("mnu_program_update");
		this.mnu_program_update
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						CreateTunnelerConfig.this
								.mnu_program_updateActionPerformed(evt);
					}
				});
		this.jMenu3.add(this.mnu_program_update);

		this.jSeparator4.setName("jSeparator4");
		this.jMenu3.add(this.jSeparator4);

		this.mnu_ask_username.setText("Ask username");
		this.mnu_ask_username.setName("mnu_ask_username");
		this.mnu_ask_username
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						CreateTunnelerConfig.this
								.mnu_ask_usernameActionPerformed(evt);
					}
				});
		this.jMenu3.add(this.mnu_ask_username);

		this.mnu_overwrite_all.setText("Overwrite all");
		this.mnu_overwrite_all.setName("mnu_overwrite_all");
		this.mnu_overwrite_all
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						CreateTunnelerConfig.this
								.mnu_overwrite_allActionPerformed(evt);
					}
				});
		this.jMenu3.add(this.mnu_overwrite_all);

		this.mnu_start_minimized.setText("Start minimized");
		this.mnu_start_minimized.setName("mnu_start_minimized");
		this.mnu_start_minimized
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						CreateTunnelerConfig.this
								.mnu_start_minimizedActionPerformed(evt);
					}
				});
		this.jMenu3.add(this.mnu_start_minimized);

		this.mnu_connect_to_all.setText("Connect to all on startup");
		this.mnu_connect_to_all.setName("mnu_connect_to_all");
		this.mnu_connect_to_all
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						CreateTunnelerConfig.this
								.mnu_connect_to_allActionPerformed(evt);
					}
				});
		this.jMenu3.add(this.mnu_connect_to_all);

		this.mnu_hide_tool_config.setText("Hide tool config");
		this.mnu_hide_tool_config.setName("mnu_hide_tool_config");
		this.mnu_hide_tool_config
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						CreateTunnelerConfig.this
								.mnu_hide_tool_configActionPerformed(evt);
					}
				});
		this.jMenu3.add(this.mnu_hide_tool_config);

		this.jSeparator5.setName("jSeparator5");
		this.jMenu3.add(this.jSeparator5);

		this.mnu_debug_mode.setText("Enable debug mode");
		this.mnu_debug_mode.setName("mnu_debug_mode");
		this.mnu_debug_mode
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						CreateTunnelerConfig.this
								.mnu_debug_modeActionPerformed(evt);
					}
				});
		this.jMenu3.add(this.mnu_debug_mode);

		this.jMenuBar1.add(this.jMenu3);

		this.jMenu4.setText("Files");
		this.jMenu4.setName("jMenu4");

		this.mnu_show_files.setText("Show files");
		this.mnu_show_files.setName("mnu_show_files");
		this.mnu_show_files
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						CreateTunnelerConfig.this
								.mnu_show_filesActionPerformed(evt);
					}
				});
		this.jMenu4.add(this.mnu_show_files);

		this.jMenuBar1.add(this.jMenu4);

		this.jMenu2.setText("Script");
		this.jMenu2.setName("jMenu2");

		this.mnu_show_variables.setText("Show variables");
		this.mnu_show_variables.setName("mnu_show_variables");
		this.mnu_show_variables
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						CreateTunnelerConfig.this
								.mnu_show_variablesActionPerformed(evt);
					}
				});
		this.jMenu2.add(this.mnu_show_variables);

		this.jSeparator3.setName("jSeparator3");
		this.jMenu2.add(this.jSeparator3);

		this.mnu_show_script.setText("Show script");
		this.mnu_show_script.setName("mnu_show_script");
		this.mnu_show_script
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						CreateTunnelerConfig.this
								.mnu_show_scriptActionPerformed(evt);
					}
				});
		this.jMenu2.add(this.mnu_show_script);

		this.jMenuBar1.add(this.jMenu2);

		this.jMenu5.setText("Other");
		this.jMenu5.setName("jMenu5");

		this.mnu_show_paths.setText("Show paths");
		this.mnu_show_paths.setName("mnu_show_paths");
		this.mnu_show_paths
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						CreateTunnelerConfig.this
								.mnu_show_pathsActionPerformed(evt);
					}
				});
		this.jMenu5.add(this.mnu_show_paths);

		this.jSeparator2.setName("jSeparator2");
		this.jMenu5.add(this.jSeparator2);

		this.jMenuItem3.setText("Servers/Tunnels");
		this.jMenuItem3.setName("jMenuItem3");
		this.jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				CreateTunnelerConfig.this
						.mnu_show_ServerTunnelsActionPerformed(evt);
			}
		});
		this.jMenu5.add(this.jMenuItem3);

		this.jMenuItem4.setText("Tools");
		this.jMenuItem4.setName("jMenuItem4");
		this.jMenu5.add(this.jMenuItem4);

		this.jMenuBar1.add(this.jMenu5);

		this.setJMenuBar(this.jMenuBar1);

		this.pack();
	}

	private void mnu_generateActionPerformed(java.awt.event.ActionEvent evt) {
		File output = this.select_output_file();

		if (output == null) {
			JOptionPane.showMessageDialog(this, "Generation aborted.");
			return;
		}

		InstallerGenerator.generate(output);
	}

	private void mnu_exitActionPerformed(java.awt.event.ActionEvent evt) {
		System.exit(0);
	}

	private void mnu_program_updateActionPerformed(
			java.awt.event.ActionEvent evt) {
		this.config_changed();
	}

	private void mnu_ask_usernameActionPerformed(java.awt.event.ActionEvent evt) {
		this.config_changed();
	}

	private void mnu_overwrite_allActionPerformed(java.awt.event.ActionEvent evt) {
		this.config_changed();
	}

	private void mnu_start_minimizedActionPerformed(
			java.awt.event.ActionEvent evt) {
		this.config_changed();
	}

	private void mnu_connect_to_allActionPerformed(
			java.awt.event.ActionEvent evt) {
		this.config_changed();
	}

	private void mnu_hide_tool_configActionPerformed(
			java.awt.event.ActionEvent evt) {
		this.config_changed();
	}

	private void mnu_debug_modeActionPerformed(java.awt.event.ActionEvent evt) {
		this.config_changed();
	}

	private void mnu_show_filesActionPerformed(java.awt.event.ActionEvent evt) {
		if (this.showFiles_frame == null) {
			this.showFiles_frame = new ShowFiles(this);
		}

		if (!this.showFiles_frame.isVisible()) {
			this.showFiles_frame.setVisible(true);
			this.desktop.add(this.showFiles_frame);
		}
	}

	private void mnu_show_ServerTunnelsActionPerformed(ActionEvent evt) {
		if (this.serverTunnel_frame == null) {
			this.serverTunnel_frame = new ManageServersTunnels(this);
		}

		if (!this.serverTunnel_frame.isVisible()) {
			this.serverTunnel_frame.setVisible(true);
			this.desktop.add(this.serverTunnel_frame);
		}
	}

	private void mnu_show_variablesActionPerformed(
			java.awt.event.ActionEvent evt) {
		if (this.showScriptVariables_frame == null) {
			this.showScriptVariables_frame = new ShowScriptVariables(this);
		}

		if (!this.showScriptVariables_frame.isVisible()) {
			this.showScriptVariables_frame.setVisible(true);
			this.desktop.add(this.showScriptVariables_frame);
		}
	}

	private void mnu_show_scriptActionPerformed(java.awt.event.ActionEvent evt) {
		if (this.showScript_frame == null) {
			this.showScript_frame = new ShowScript(this);
		}

		if (!this.showScript_frame.isVisible()) {
			this.showScript_frame.setVisible(true);
			this.desktop.add(this.showScript_frame);
		}
	}

	private void mnu_show_pathsActionPerformed(java.awt.event.ActionEvent evt) {
		if (this.showPaths_frame == null) {
			this.showPaths_frame = new ShowPaths(this);
		}

		if (!this.showPaths_frame.isVisible()) {
			this.showPaths_frame.setVisible(true);
			this.desktop.add(this.showPaths_frame);
		}
	}

	public File select_output_file() {
		JFileChooser chooser = new JFileChooser();
		chooser.setMultiSelectionEnabled(false);
		ExtensionFilter filter = new ExtensionFilter("Java executable file");
		filter.add_accepted_extension(".jar");
		chooser.setCurrentDirectory(new File("../export"));
		chooser.setFileFilter(filter);

		chooser.showSaveDialog(this);

		File selection = chooser.getSelectedFile();
		if (selection == null) {
			return null;
		}

		if (!selection.getPath().endsWith(".jar")) {
			selection = new File(selection.getPath() + ".jar");
		}

		return selection;
	}

	public void config_changed() {
		if (!TunnelerConfig.inst.isProg_update()) {
			TunnelerConfig.inst.setAsk_username(this.mnu_ask_username
					.isSelected());
			TunnelerConfig.inst.setDebug_link(this.mnu_debug_mode.isSelected());
			TunnelerConfig.inst
					.setConnect_to_all_on_start(this.mnu_connect_to_all
							.isSelected());
			TunnelerConfig.inst.setStart_minimized(this.mnu_start_minimized
					.isSelected());
			TunnelerConfig.inst.setHide_tool_config(this.mnu_hide_tool_config
					.isSelected());
			TunnelerConfig.inst.setOverwrite_all(this.mnu_overwrite_all
					.isSelected());
		}
		TunnelerConfig.inst
				.setProg_update(this.mnu_program_update.isSelected());

		this.fire_config_changed();
	}

	public void refresh() {
		this.mnu_hide_tool_config.setSelected(TunnelerConfig.inst
				.isHide_tool_config());
		this.mnu_overwrite_all.setSelected(TunnelerConfig.inst
				.isOverwrite_all());
		this.mnu_start_minimized.setSelected(TunnelerConfig.inst
				.isStart_minimized());
		this.mnu_connect_to_all.setSelected(TunnelerConfig.inst
				.isConnect_to_all_on_start());
		this.mnu_debug_mode.setSelected(TunnelerConfig.inst.isDebug_link());
		this.mnu_ask_username.setSelected(TunnelerConfig.inst.isAsk_username());
		this.mnu_program_update
				.setSelected(TunnelerConfig.inst.isProg_update());

		if (TunnelerConfig.inst.isProg_update()) {
			this.mnu_hide_tool_config.setEnabled(false);
			this.mnu_overwrite_all.setEnabled(false);
			this.mnu_start_minimized.setEnabled(false);
			this.mnu_connect_to_all.setEnabled(false);
			this.mnu_debug_mode.setEnabled(false);
			this.mnu_ask_username.setEnabled(false);
		} else {
			this.mnu_hide_tool_config.setEnabled(true);
			this.mnu_overwrite_all.setEnabled(true);
			this.mnu_start_minimized.setEnabled(true);
			this.mnu_connect_to_all.setEnabled(true);
			this.mnu_debug_mode.setEnabled(true);
			this.mnu_ask_username.setEnabled(true);
		}
	}

	private javax.swing.JDesktopPane desktop;
	private javax.swing.JMenu jMenu1;
	private javax.swing.JMenu jMenu2;
	private javax.swing.JMenu jMenu3;
	private javax.swing.JMenu jMenu4;
	private javax.swing.JMenu jMenu5;
	private javax.swing.JMenuBar jMenuBar1;
	private javax.swing.JMenuItem jMenuItem3;
	private javax.swing.JMenuItem jMenuItem4;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JSeparator jSeparator1;
	private javax.swing.JSeparator jSeparator2;
	private javax.swing.JSeparator jSeparator3;
	private javax.swing.JSeparator jSeparator4;
	private javax.swing.JSeparator jSeparator5;
	private javax.swing.JCheckBoxMenuItem mnu_ask_username;
	private javax.swing.JCheckBoxMenuItem mnu_connect_to_all;
	private javax.swing.JCheckBoxMenuItem mnu_debug_mode;
	private javax.swing.JMenuItem mnu_exit;
	private javax.swing.JMenuItem mnu_generate;
	private javax.swing.JCheckBoxMenuItem mnu_hide_tool_config;
	private javax.swing.JCheckBoxMenuItem mnu_overwrite_all;
	private javax.swing.JCheckBoxMenuItem mnu_program_update;
	private javax.swing.JMenuItem mnu_show_files;
	private javax.swing.JMenuItem mnu_show_paths;
	private javax.swing.JMenuItem mnu_show_script;
	private javax.swing.JMenuItem mnu_show_variables;
	private javax.swing.JCheckBoxMenuItem mnu_start_minimized;

	public static void main(String args[]) {
		try {
			// Set cross-platform Java L&F (also called "Metal")
			UIManager.setLookAndFeel(
			// UIManager.getCrossPlatformLookAndFeelClassName());
					UIManager.getSystemLookAndFeelClassName());
		} catch (UnsupportedLookAndFeelException e) {
			System.out.print("Unsupported L&F");
		} catch (ClassNotFoundException e) {
			System.out.print("Could not find L&F");
		} catch (InstantiationException e) {
			System.out.print("Error in L&F");
		} catch (IllegalAccessException e) {
			System.out.print("Error in L&F");
		}

		/* Force loading of paths */
		Config c = Config.inst;
		if(!c.validate_config())
		{
			JOptionPane.showMessageDialog(null, "Could not automaticaly determine the paths for SSHTunneler's other tools. Please set them before generation.");
		}
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new CreateTunnelerConfig().setVisible(true);
			}
		});
	}
}
